package com.syqc.monitor.ui.realalarm

import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import autodispose2.autoDispose
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.syqc.comlib.base.BaseRecycleActivity
import com.syqc.comlib.utils.ScreenUtil
import com.syqc.comlib.utils.ToolbarHelper
import com.syqc.comlib.utils.loadGif
import com.syqc.comlib.utils.showToast
import com.syqc.comlib.utils.startActivityTo
import com.syqc.comlib.utils.toast
import com.syqc.custom.LeftRightTextView
import com.syqc.entity.AlarmDetail
import com.syqc.monitor.R
import com.syqc.monitor.databinding.ActivityToolbarListBinding
import com.syqc.net.ServiceHelper
import com.syqc.utils.Keys
import com.syqc.utils.TimeUtil
import com.syqc.utils.ToolsUtil

/**
 *@Author Jasper
 *@Time   2020/12/1
 *@Desc   报警类型列表
 */
class AlarmInfoActivity :
    BaseRecycleActivity<ActivityToolbarListBinding>(ActivityToolbarListBinding::inflate) {
    private val mAdapter by lazy { MAdapter() }
    private val emptyView by lazy {
        val view = layoutInflater.inflate(
            R.layout.layout_empty_view,
            mRecyclerVB.recyclerView,
            false
        )
        view.findViewById<ImageView>(R.id.ivNoData).loadGif(this, R.mipmap.ic_empty)

        view
    }

    override fun initView() {
        super.initView()
        ToolbarHelper.setToolbar(this, intent.getStringExtra(Keys.INTENT_TITLE) ?: "")
        mRecyclerVB.recyclerView.layoutManager = LinearLayoutManager(this)
        mRecyclerVB.recyclerView.setPadding(0, ScreenUtil.dp2px(this, 12), 0, 0)
        mRecyclerVB.recyclerView.adapter = mAdapter
        val alarmId = intent.getStringExtra(Keys.INTENT_ALARM_ID)
        if (alarmId.isNullOrEmpty()) return
        getAlarmByType(alarmId)
    }

    override fun setListener() {
        mAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (ToolsUtil.isFastClick()) return@setOnItemChildClickListener
            val entity = mAdapter.data[position]
            getAlarmInfo(entity.carId, entity.time, entity.alarmType, entity.carPlate)
        }
    }

    private fun getAlarmByType(alarmType: String) {
        isShowLoading(true)
        ServiceHelper.getRepository().getAlarmDetail(alarmType).autoDispose(scopeProvider)
            .subscribe({
                isShowLoading(false)
                if (it.isNullOrEmpty()) {
                    toast(R.string.desc_no_about_data)
                    mAdapter.setEmptyView(emptyView)
                    return@subscribe
                }
                mAdapter.setList(it)
            }, {
                showToast(it.message ?: "")
                isShowLoading(false)
            })
    }

    private fun getAlarmInfo(catId: String, startTime: String, alarmType: String, plate: String) {
        isShowLoading(true)
        ServiceHelper.getRepository().getAlarmMedia(
            catId,
            TimeUtil.instance.formatHm(startTime),
            alarmType
        )
            .autoDispose(scopeProvider).subscribe({
                isShowLoading(false)
                if (it.isNullOrEmpty()) {
                    toast(R.string.desc_no_about_data)
                    return@subscribe
                }

                startActivityTo<MediaInfoActivity>(this) {
                    putExtra(Keys.INTENT_TITLE, plate)
                    putExtra(Keys.INTENT_CAR_ID, catId)
                    putParcelableArrayListExtra(Keys.INTENT_DATA, it)
                }
            }, {
                showToast(it.message ?: "")
                isShowLoading(false)
            })
    }


    inner class MAdapter :
        BaseQuickAdapter<AlarmDetail, BaseViewHolder>(R.layout.item_alarm_info) {
        init {
            addChildClickViewIds(R.id.iv_alarm_info_pop)
        }

        override fun convert(holder: BaseViewHolder, item: AlarmDetail) {
            item.run {
                holder.setVisible(
                    R.id.iv_alarm_info_pop,
                    !fjNum.isNullOrEmpty() && fjNum.toInt() > 0
                )
                holder.setText(R.id.tv_alarm_info_car, carPlate)
                    .setText(R.id.tv_alarm_info_address, addr)
                holder.getView<LeftRightTextView>(R.id.lr_tv_alarm_belong)
                    .setRightText(teamName ?: "")
                holder.getView<LeftRightTextView>(R.id.lr_tv_alarm_info_time)
                    .setRightText(time)
                holder.getView<LeftRightTextView>(R.id.lr_tv_alarm_info_speed)
                    .setRightText("$speed ${getString(R.string.unit_speed)}")
            }
        }
    }
}